<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索 -->
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.key"
            placeholder="根据编号搜索"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="searchForm.key = ''">清除</el-button>
          <el-button size="mini" type="primary" @click="onSubmit"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 信息 -->
      <el-alert type="info" :closable="false">
        <span class="alertInfo">
          <i class="el-icon-info"></i>
          <span>数据一共 {{ counts }} 条</span>
        </span>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="randomsList" style="width: 100%">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="questionType" label="题型" width="70px">
          <template slot-scope="scope">
            {{ changeType(scope.row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号">
          <template slot-scope="scope">
            <el-link
              v-for="(item, index) in scope.row.questionIDs"
              :key="index"
              type="primary"
              @click="showDialog(item.id)"
              >{{ item.number }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间"> </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"> </el-table-column>
        <el-table-column label="操作" width="100px">
          <template slot-scope="scope">
            <el-button
              type="danger"
              @click="delHandler(scope.row)"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->

      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageForm.page"
          :page-sizes="[2, 5, 10]"
          :page-size="pageForm.pagesize"
          layout="prev, pager, next,sizes,jumper"
          :total="counts"
          background
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="提示" :visible.sync="dialogVisible">
      <questionsPreview v-if="dialogVisible" :id="id"></questionsPreview>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import questionsPreview from '@/module-hmmm/components/questions-preview.vue'
export default {
  name: 'questionsRandoms',
  components: {
    questionsPreview
  },
  data () {
    return {
      dialogVisible: false,
      searchForm: {
        key: ''
      },
      pageForm: {
        page: 1,
        pagesize: 2
      },
      counts: 0,
      randomsList: [],
      id: ''
    }
  },
  created () {
    this.getRandomsList()
  },
  methods: {
    // 搜索提交
    onSubmit () {
      // console.log(this.searchForm.key.length)
      // if (!this.searchForm.key.length) return this.$message.error('不能为空')
      this.getRandomsList()
    },
    // 获取组题列表
    async getRandomsList () {
      const res = await randoms({
        ...this.pageForm,
        keyword: this.searchForm.key
      })
      // console.log(res)
      if (res.status !== 200) return
      this.randomsList = res.data.items
      this.counts = res.data.counts
    },
    // 打开Dialog
    showDialog (id) {
      this.dialogVisible = true
      this.id = id
      // console.log(id)
    },
    // 题目类型数据转换
    changeType (value) {
      if (+value === 1) return '单选'
      if (+value === 2) return '多选'
      if (+value === 3) return '简答'
    },
    // 分页
    handleSizeChange (val) {
      this.pageForm.pagesize = val
      this.getRandomsList()
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getRandomsList()
    },
    // 删除
    delHandler (row) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await removeRandoms(row)
          if (res.status !== 200) return
          if (this.randomsList.length <= 1 && this.pageForm.page > 2) {
            this.pageForm.page--
          }
          this.getRandomsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.el-form {
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
}
::v-deep .el-form .el-input__inner {
  width: 330px;
  height: 30px;
}
.alertInfo {
  font-size: 14px;
}
.alertInfo i {
  margin-right: 10px;
}
.el-alert {
  height: 30px;
}
.el-table {
  margin-top: 30px;
}

.el-button--danger {
  color: #f00;
  background-color: #fbf1f1;
  border-color: #f1dfdf;
}
.el-button--danger:focus,
.el-button--danger:hover {
  background: #f78989;
  border-color: #f78989;
  color: #fff;
}
.page {
  margin-top: 40px;
  text-align: right;
}
</style>
